'''
LSA Reduced Context
yields a reduced context calculated from U and Sigma.
The resultant context contains a matrix of the original amount of rows but a reduced amount of columns.
The new amount of columns is the quantity of dimensions given by parameter.
'''
import sys
from statistic_functions import *
from lsa_functions import *
from context_functions import *
from numpy import mean

if __name__ == '__main__':
	
	if len(sys.argv)<2: 
		print "\n*** usage:",sys.argv[0]," [dbfile] [dimensions] [offset]"
		exit()
	dimensions = int(sys.argv[2])
	offset = float(sys.argv[3])
	dbfile = sys.argv[1]

	print "Leyendo matriz"	
	matrix,extent,intent = readContext(dbfile)
	
	print "Creando LSA"
	#Create
	lsa = LSA(matrix)
	#print lsa
	print "Calculando tfidf"
	#Prepare
	lsa.tfidfTransform()
	#print lsa.matrix
	print "Calculando svd with " + str(dimensions) + " dimensions"
	#Perform
	lsa.lsaTransform(dimensions)
	#print lsa
	
	print "Escribiendo nuevo contexto para valores superiores a " + str(offset)
	salida = ""
	salida_nb=""
	fs1 = []
	dims = ""
	for i in range(dimensions):
		dims += "d"+str(i+1)+" | "
	dims += "\n"
	averages = lsa.matrix.mean(axis=0)
	stds = lsa.matrix.std(axis=0)
	limits = []
	for i in lsa.matrix.T:
		if sum(i) != 0:
			limits.append(search_limits(i, offset))
		else:
			limits.append([0,0])

	salida_bin = []
	for i in lsa.matrix:
		ctr=0
		row_bin = []
		for k in i:
			if k >= limits[ctr][0] and k <= limits[ctr][1]:
				row_bin.append(1)
			else:
				row_bin.append(0)
			ctr += 1
		salida_bin.append(row_bin)
	salida_bin = array(salida_bin)
	
	dbfile = dbfile[dbfile.rfind("/")+1:]
	intent = []
	for i in range(dimensions):
		intent.append("d"+str(i))
	
	f1 = dbfile.replace(".rcf","")+"-k"+str(dimensions) +"-a"+ str(offset) +"-binary.rcf"
	writeContext(f1,salida_bin,extent,intent,path="contextos/")
	
	f1 = dbfile.replace(".rcf","")+"-k"+str(dimensions) +"-a"+ str(offset) +"-binary-inv.rcf"
	writeContext(f1,salida_bin.T,intent,extent,path="contextos/")
	
	f1 = dbfile.replace(".rcf","")+"-k"+str(dimensions) +"values.rcf"
	writeContext(f1,lsa.matrix,extent,intent,path="contextos/")
	
	#print "Estadisticas"
	#print "Estado Original: "
	#print "--> Valores distintos de 0: " + str(len(fs0))
	#print "--> Promedio de los valores: " + str(numpy.mean(fs0))

	#print "Estado Modificado: "
	#print "--> Valores distintos de 0: " + str(len(fs1))
	#print "--> Promedio de los valores: " + str(numpy.mean(fs1))
	
	#perc = float(len(fs1))/float(lsa.matrix.size)
	#print "Densidad del contexto final en " + str(perc) + "%"
	print "Ejecucion finalizada"
	
